package web.controller;

import hibernate.pojo.User;
import hibernate.util.HibernateUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import web.controller.address_telefon.CreateNewTelephoneBookController;

public class InsertNewUserController {

	private static final Log logger = LogFactory.getLog(CreateNewTelephoneBookController.class);
	
	private String firstName;
	private String lastName;
	private String userName;
	private String password;
	private User user;
	
	public InsertNewUserController() {
		// TODO Auto-generated constructor stub
	}

	
	public String insertNewUser(){
		
		String pageToRedirect = "";
		
		if(!validateUserInput()){
			
			pageToRedirect = "";
			return pageToRedirect;
		}
		
		try {

			user = new User();
			user.setFirstName(firstName);
			user.setLastName(lastName);
			user.setUserName(userName);
			user.setPassword(password);
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			Transaction transaction = session.beginTransaction();
			session.save(user);
			transaction.commit();
			clearFields();
			pageToRedirect = "insertNewUserSuccess";
			
		} catch (Exception exception) {
			logger.error("Yeni kullanici kaydederken hata olustu!", exception);
		}
		
		
		return pageToRedirect;
		
		
	}
	
	private boolean validateUserInput(){
		
		return true;
	}
	
	private void clearFields(){
		firstName = "";
		lastName = "";
		userName = "";
		password = "";
		
	}
	
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}
	
}
